package com.example.xlmkotlinwanandroid.ext

import android.app.ActionBar
import android.view.View
import android.view.ViewGroup
import androidx.core.view.marginLeft
import java.util.logging.Handler

/**
 *  author : cailei
 *  date : 2022/3/10
 *  description :
 */

fun View.updateMargin(
    left: Int? = null,
    top: Int? = null,
    right: Int? = null,
    bottom: Int? = null
) {
    (layoutParams as ViewGroup.MarginLayoutParams).let { param ->
        updateLayoutParams<ViewGroup.MarginLayoutParams> {
            left?.let {
                marginStart = left
            }
            right?.let {
                marginEnd = right
            }
            top?.let {
                topMargin = top
            }
            bottom?.let {
                bottomMargin = bottom
            }
        }
    }

}

fun <T : ViewGroup.LayoutParams> View.updateLayoutParams(block: T.() -> Unit) {
    val param = layoutParams as T
    block(param)
    layoutParams = param
}